package com.alibaba.citrus.ots.application.report.ability.utils;

import com.alibaba.citrus.ots.common.util.DateUtils;
import com.epoch.app.otsapplicationreportsdk.model.dto.DataInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

public class DomUtil {

    public final static int SIXTY = 60;
    public final static int FIFTEEN = 15;
    public final static int ONE_HUNDRED_MILLION = 100000000;
    public final static int ONE_HUNDRED_THOUSAND = 100000;
    public final static int ONE_THOUSAND = 1000;
    public final static int TEN_THOUSAND = 10000;
    public final static String JIAN = "件";
    public final static String WAN_JIAN = "万件";
    public final static String YI_JIAN = "亿件";
    public final static String DAN = "单";
    public final static String WAN_DAN = "万单";
    public final static String YI_DAN = "亿单";
    public final static String YUAN = "件";
    public final static String WAN_YUAN = "万件";
    public final static String YI_YUAN = "亿件";
    public final static String DATE_FORMAT = "yyyy-MM-dd";
    public final static BigDecimal ONE_THOUSAND_BIGDECIMAL = new BigDecimal(1000);
    public final static BigDecimal ONE_HUNDRED = new BigDecimal(100);

    //当前日期-生产日期 超1/4保质期
    public static Boolean exceedFourthExpired(String dom, Integer guaranteePeriod){
        return !lessFourthExpired(dom,guaranteePeriod) &&
                !exceedThirdExpired(dom,guaranteePeriod);
    }
    //当前日期-生产日期 超1/3保质期
    public static Boolean exceedThirdExpired(String dom, Integer guaranteePeriod){
        int diff = compTodayDiff(dom);
        return diff >= guaranteePeriod/3;
    }
    //当前日期-生产日期 小于1/4保质期
    public static Boolean lessFourthExpired(String dom, Integer guaranteePeriod){
        int diff = compTodayDiff(dom);
        return diff < guaranteePeriod/4;
    }
    //临近保质期满
    public static Boolean approachExpired(String dom, Integer guaranteePeriod){
        int diff = compTodayDiff(dom);
        return diff >= guaranteePeriod*2/3;
    }
    //临近保质期满
    public static Boolean approachExpired4Ec(String dom, Integer guaranteePeriod){
        String future = getRollbackDate(new Date(), FIFTEEN);
        int diff = getBetweenDays(dom,future);
        return (diff >= guaranteePeriod/3) && !exceedThirdExpired(dom,guaranteePeriod);
    }
    //生产日期为三个月前
    public static Boolean threeMonthsAgo(String dom){
        return compCurrMonthDiff(dom) >= 3;
    }
    //生产日期二个月前上旬
    public static Boolean twoMonthsAgoEarly(String dom){
        return compCurrMonthDiff(dom) == 2 &&
                earlyOfMonth(dom);
    }
    //生产日期二个月前中旬
    public static Boolean twoMonthsAgoMid(String dom){
        return compCurrMonthDiff(dom) == 2 &&
                midOfMonth(dom);
    }
    //生产日期二个月前下旬
    public static Boolean twoMonthsAgoLate(String dom){
        return compCurrMonthDiff(dom) == 2 &&
                lateOfMonth(dom);
    }
    //生产日期一个月前上旬
    public static Boolean oneMonthAgoEarly(String dom){
        return compCurrMonthDiff(dom) == 1 &&
                earlyOfMonth(dom);
    }
    //生产日期一个月前中旬
    public static Boolean oneMonthAgoMid(String dom){
        return compCurrMonthDiff(dom) == 1 &&
                midOfMonth(dom);
    }
    //生产日期一个月前下旬
    public static Boolean oneMonthAgoLate(String dom){
        return compCurrMonthDiff(dom) == 1 &&
                lateOfMonth(dom);
    }
    //生产日期当月上旬
    public static Boolean thisMonthEarly(String dom){
        return compCurrMonthDiff(dom) == 0 &&
                earlyOfMonth(dom);
    }
    //生产日期当月中旬
    public static Boolean thisMonthMid(String dom){
        return compCurrMonthDiff(dom) == 0 &&
                midOfMonth(dom);
    }
    //生产日期当月下旬
    public static Boolean thisMonthLate(String dom){
        return compCurrMonthDiff(dom) == 0 &&
                lateOfMonth(dom);
    }
    //生产日期当月
    public static Boolean thisMonth(String dom){
        return compCurrMonthDiff(dom) == 0;
    }
    //生产日期前一个月
    public static Boolean oneMonth(String dom){
        return compCurrMonthDiff(dom) == 1;
    }
    //生产日期前二个月
    public static Boolean twoMonths(String dom){
        return compCurrMonthDiff(dom) == 2;
    }
    //生产日期为前三个月
    public static Boolean threeMonths(String dom){
        return compCurrMonthDiff(dom) == 3;
    }
    //生产日期为四个月以前
    public static Boolean fourMonthsAgo(String dom){
        return compCurrMonthDiff(dom) >= 4;
    }
    //计算当天与生产日期的差值(正数)
    public static int compTodayDiff(String dom) {
        return getBetweenDays(dom,getToday());
    }
    //计算当前与生产日期月份差（正数）
    public static int compCurrMonthDiff(String dom) {
        Calendar now = Calendar.getInstance();
        Calendar pro = Calendar.getInstance();
        pro.setTime(DateUtils.parse(dom));
        int year = (now.get(Calendar.YEAR)-pro.get(Calendar.YEAR))*12;
        int month = now.get(Calendar.MONTH)-pro.get(Calendar.MONTH);
        return year+month;
    }
    //判断是否为上旬
    public static Boolean earlyOfMonth(String dom){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parse(dom));
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day <= 10;
    }
    //判断是否为中旬
    public static Boolean midOfMonth(String dom){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parse(dom));
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day > 10 && day < 21;
    }
    //判断是否为下旬
    public static Boolean lateOfMonth(String dom){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parse(dom));
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day > 20;
    }
    //获取当天日期格式 yyyy-MM-dd
    public static String getToday() {
        Date date = new Date();
        return DateFormatUtils.format(date, DATE_FORMAT);
    }
    //获取昨天的日期：格式 yyyy-MM-dd
    public static String getYesterday(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);
        return DateFormatUtils.format(cal.getTime(), DATE_FORMAT);
    }
    //获取昨天的日期：格式 yyyy-MM-dd
    public static String getYesterdayByDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        return DateFormatUtils.format(cal.getTime(), DATE_FORMAT);
    }
    //获取昨天的日期：格式 yyyy-MM-dd
    public static Date getYesterdayTimeByDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }
    //取上月最大天数
    public static int getMaxDayLastMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.MONTH,-1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    //取本月当天数
    public static int getTodayOfMonth(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }
    //取上月第一天
    public static Date getFirstDayLastMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.MONTH,-1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    //取上月最后一天
    public static String getLastDayLastMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.MONTH,-1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateFormatUtils.format(calendar.getTime(), DATE_FORMAT);
    }
    //取本月最后一天
    public static String getLastDayMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateFormatUtils.format(calendar.getTime(), DATE_FORMAT);
    }
    //取本月第一天
    public static Date getStartTimeOfMonth(Date date) {
        Calendar startThisMonth = Calendar.getInstance();
        startThisMonth.setTime(date);
        startThisMonth.set(Calendar.DAY_OF_MONTH, 1);
        startThisMonth.set(Calendar.HOUR_OF_DAY, 0);
        startThisMonth.set(Calendar.MINUTE, 0);
        startThisMonth.set(Calendar.SECOND, 0);
        return startThisMonth.getTime();
    }
    //取本月第一天
    public static String getStartOfMonth(Date date) {
        Calendar startThisMonth = Calendar.getInstance();
        startThisMonth.setTime(date);
        startThisMonth.set(Calendar.DAY_OF_MONTH, 1);
        return DateFormatUtils.format(startThisMonth.getTime(), DATE_FORMAT);
    }
    //获取指定日期之前或之后的日期
    public static String getRollbackDate(Date date,int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return DateFormatUtils.format(cal.getTime(), DATE_FORMAT);
    }
    //是否是月的最后一天
    public static boolean isLastDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    //是否是月的第一天
    public static boolean isFirstDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }
    //判断日期是否相同
    public static boolean equalsDate(Date date1,Date date2){
        if(date1 == null || date2 == null){
            return false;
        }
        String d1 =  DateFormatUtils.format(date1, DATE_FORMAT);
        String d2 =  DateFormatUtils.format(date2, DATE_FORMAT);
        return d1.equals(d2);
    }
    //计算小数，精确到四位
    public static String numberFormat(Long part, Long total){
        if (total==null || total==0){
            return "0.0000";
        }
        DecimalFormat df = new DecimalFormat("#0.0000");
        return df.format((float) part / (float) total);
    }
    //计算百分比精确到两位
    public static DataInfo decimalFormat(Long part, Long total){
        DataInfo dataInfo = new DataInfo();
        dataInfo.setUnit("%");
        if (total==null || total==0 || part==null || part==0){
            dataInfo.setValue("0");
            dataInfo.setTail(".00");
            return dataInfo;
        }
        DecimalFormat df = new DecimalFormat("#.00");
        String[] array = df.format((float) part / (float) total*100).split("\\.");
        dataInfo.setValue(StringUtils.leftPad(array[0],1,"0"));
        dataInfo.setTail("."+array[1]);
        return dataInfo;
    }
    //数量格式化（千分位+小数点2位+单位）
    public static DataInfo quantityFormat(Long quantity){
        if (quantity.longValue()/ONE_THOUSAND>=ONE_HUNDRED_MILLION){
            return convert2DataInfo(quantity,ONE_HUNDRED_MILLION,YI_JIAN);
        }
        else if (quantity.longValue()/ONE_THOUSAND>=ONE_HUNDRED_THOUSAND){
            return convert2DataInfo(quantity,TEN_THOUSAND,WAN_JIAN);
        }else {
            return convert2DataInfo(quantity,1,JIAN);
        }
    }

    public static String quantityFormatWan(Long quantity){
        if(Long.valueOf(0).equals(quantity)){
            return "0.00";
        }
        BigDecimal first = new BigDecimal(quantity);
        BigDecimal big = new BigDecimal(TEN_THOUSAND);
        BigDecimal ths = new BigDecimal(ONE_THOUSAND);
        BigDecimal ams = big.multiply(ths);
        BigDecimal result = first.divide(ams).setScale(2, BigDecimal.ROUND_HALF_UP);
        DecimalFormat integer = new DecimalFormat("#####,##0");
        return integer.format(result)+"."+result.toString().split("\\.")[1];
    }

    public static String quantityFormatDecimal(BigDecimal quantity){
        if(quantity == null || quantity.compareTo(BigDecimal.ZERO)==0){
            return "0.00";
        }
        DecimalFormat integer = new DecimalFormat("#####,##0");
        return integer.format(quantity)+"."+quantity.toString().split("\\.")[1];
    }

    public static String quantityFormatJian(Long quantity){
        if(Long.valueOf(0).equals(quantity)){
            return "0.000";
        }
        BigDecimal first = new BigDecimal(quantity);
        BigDecimal big = new BigDecimal(1);
        BigDecimal ths = new BigDecimal(ONE_THOUSAND);
        BigDecimal ams = big.multiply(ths);
        BigDecimal result = first.divide(ams).setScale(3, BigDecimal.ROUND_HALF_UP);
        DecimalFormat integer = new DecimalFormat("#####,##0");
        return integer.format(result.intValue())+"."+result.toString().split("\\.")[1];
    }

    //数量格式化（千分位+小数点2位+单位）
    public static DataInfo quantityFormatForForecastOrder(Long quantity){
        if (quantity.longValue()/ONE_THOUSAND>=ONE_HUNDRED_MILLION){
            return convert2DataInfoForDan(quantity,ONE_HUNDRED_MILLION,YI_DAN);
        }
        else if (quantity.longValue()/ONE_THOUSAND>=ONE_HUNDRED_THOUSAND){
            return convert2DataInfoForDan(quantity,TEN_THOUSAND,WAN_DAN);
        }else {
            return convert2DataInfoForDan(quantity,1,DAN);
        }
    }

    public static DataInfo quantityFormatForJian(Long quantity){
        if (quantity.longValue()>=ONE_HUNDRED_THOUSAND){
            return convert2DataInfoForJian(quantity, TEN_THOUSAND, WAN_JIAN);
        }else {
            return convert2DataInfoForJian(quantity,1, JIAN);
        }
    }

    public static DataInfo moneyFormatForYuan(BigDecimal money){
        if (money.compareTo(new BigDecimal(ONE_HUNDRED_THOUSAND))>=0){
            return convert2DataInfoForYuan(money, TEN_THOUSAND, WAN_YUAN);
        }else {
            return convert2DataInfoForYuan(money,1, YUAN);
        }
    }

    private static DataInfo convert2DataInfoForJian(Long quantity, int div, String unit){
        DataInfo dataInfo = new DataInfo();
        if(Long.valueOf(0).equals(quantity)){
            dataInfo.setValue("0");
            dataInfo.setTail(".00");
            dataInfo.setUnit(unit);
            return dataInfo;
        }
        BigDecimal first = new BigDecimal(quantity);
        BigDecimal big = new BigDecimal(div);
        BigDecimal result = first.divide(big).setScale(2, BigDecimal.ROUND_HALF_UP);
        DecimalFormat integer =new DecimalFormat("#####,##0");
        dataInfo.setValue(integer.format(result.intValue()));
        dataInfo.setTail("."+result.toString().split("\\.")[1]);
        dataInfo.setUnit(unit);
        return dataInfo;
    }

    private static DataInfo convert2DataInfoForYuan(BigDecimal money, int div, String unit){
        DataInfo dataInfo = new DataInfo();
        if(money.compareTo(BigDecimal.ZERO) == 0){
            dataInfo.setValue("0");
            dataInfo.setTail(".00");
            dataInfo.setUnit(unit);
            return dataInfo;
        }
        BigDecimal big = new BigDecimal(div);
        BigDecimal result = money.divide(big).setScale(2, BigDecimal.ROUND_HALF_UP);
        DecimalFormat integer =new DecimalFormat("#####,##0");
        dataInfo.setValue(integer.format(result.intValue()));
        dataInfo.setTail("."+result.toString().split("\\.")[1]);
        dataInfo.setUnit(unit);
        return dataInfo;
    }

    private static DataInfo convert2DataInfoForDan(Long quantity, int div, String unit){
        DataInfo dataInfo = new DataInfo();
        if(Long.valueOf(0).equals(quantity)){
            dataInfo.setValue("0");
            dataInfo.setTail(".00");
            dataInfo.setUnit(unit);
            return dataInfo;
        }
        BigDecimal first = new BigDecimal(quantity);
        double doubleValue = first.doubleValue();
        String[] array = String.valueOf(doubleValue).split("\\.");
        DecimalFormat integer =new DecimalFormat("#####,##0");
        dataInfo.setValue(integer.format(Long.valueOf(array[0])));
        dataInfo.setTail("."+StringUtils.leftPad(array[1],2,"0"));
        dataInfo.setUnit(unit);
        return dataInfo;
    }

    private static DataInfo convert2DataInfo(Long quantity, int div, String unit){
        DataInfo dataInfo = new DataInfo();
        if(Long.valueOf(0).equals(quantity)){
            dataInfo.setValue("0");
            dataInfo.setTail(".00");
            dataInfo.setUnit(unit);
            return dataInfo;
        }
        BigDecimal first = new BigDecimal(quantity);
        BigDecimal big = new BigDecimal(div);
        BigDecimal ths = new BigDecimal(ONE_THOUSAND);
        BigDecimal ams = big.multiply(ths);
        BigDecimal result = first.divide(ams).setScale(2, BigDecimal.ROUND_HALF_UP);
        DecimalFormat integer =new DecimalFormat("#####,##0");
        dataInfo.setValue(integer.format(result.intValue()));
        dataInfo.setTail("."+result.toString().split("\\.")[1]);
        dataInfo.setUnit(unit);
        return dataInfo;
    }

    public static String thousandthFormat(Long total){
        if (ObjectUtils.isEmpty(total)) {
            return "0";
        }
        BigDecimal totalDec = new BigDecimal(total);
        BigDecimal thousDec = new BigDecimal(ONE_THOUSAND);
        DecimalFormat integer =new DecimalFormat("#####,##0");
        BigDecimal bigDecimal = totalDec.divide(thousDec, 3, BigDecimal.ROUND_HALF_UP);
        String[] targets = bigDecimal.toString().split("\\.");
        if ("000".equals(targets[1])){
            return integer.format(bigDecimal.longValue());
        }else{
            return integer.format(bigDecimal.longValue()).concat(".").concat(targets[1]);
        }
    }

    public static String thousandthFormat2(Long total,Boolean isExcel){
        if (ObjectUtils.isEmpty(total)) {
            return "0";
        }
        BigDecimal totalDec = new BigDecimal(total);
        BigDecimal thousDec = new BigDecimal(ONE_THOUSAND);
        BigDecimal bigDecimal = totalDec.divide(thousDec, 3, BigDecimal.ROUND_HALF_UP);
        String[] targets = bigDecimal.toString().split("\\.");
        if(isExcel){
            if ("000".equals(targets[1])){
                return String.valueOf(bigDecimal.longValue());
            }else{
                return String.valueOf(bigDecimal.longValue()).concat(".").concat(targets[1]);
            }
        }
        DecimalFormat integer =new DecimalFormat("#####,##0");
        if ("000".equals(targets[1])){
            return integer.format(bigDecimal.longValue());
        }else{
            return integer.format(bigDecimal.longValue()).concat(".").concat(targets[1]);
        }
    }

    public static String stringFormat(Long total){
        if (ObjectUtils.isEmpty(total)) {
            return "0";
        }
        BigDecimal totalDec = new BigDecimal(total);
        BigDecimal thousDec = new BigDecimal(ONE_THOUSAND);
        //DecimalFormat integer =new DecimalFormat("#####,##0");
        BigDecimal bigDecimal = totalDec.divide(thousDec, 3, BigDecimal.ROUND_HALF_UP);
        String[] targets = bigDecimal.toString().split("\\.");
        if ("000".equals(targets[1])){
            return String.valueOf(bigDecimal.longValue());
        }else{
            return String.valueOf(bigDecimal.longValue()).concat(".").concat(targets[1]);
        }
    }

    //金额保留万
    public static BigDecimal bigDecimalFormatWan(BigDecimal amount,Integer scale){
        if (amount == null){
            return new BigDecimal(0);
        }
        return amount.divide(new BigDecimal(DomUtil.TEN_THOUSAND),scale,BigDecimal.ROUND_HALF_UP);
    }

    //金额格式化
    public static String amountFormat(BigDecimal amount){
        if(amount == null || amount.compareTo(BigDecimal.ZERO)==0) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("###,##0.00");
        return df.format(amount);
    }
    //金额格式化
    public static String amountFormatExcel(BigDecimal amount,Boolean isExcel){
        if(amount == null || amount.compareTo(BigDecimal.ZERO)==0) {
            return "0.00";
        }
        if(isExcel){
            return amount.toString();
        }
        DecimalFormat df = new DecimalFormat("##,##0.00");
        return df.format(amount);
    }
    //金额格式化
    public static String amountFormatWan(BigDecimal amount){
        if(amount == null || amount.compareTo(BigDecimal.ZERO)==0) {
            return "0";
        }
        BigDecimal result = amount.divide(new BigDecimal(10000),0,BigDecimal.ROUND_HALF_UP);
        DecimalFormat df = new DecimalFormat("###,###");
        return df.format(result);
    }
    //数量格式化
    public static String numFormat(Long number){
        if(number == null || number == 0) {
            return "0";
        }
        DecimalFormat df = new DecimalFormat("###,###");
        return df.format(number);
    }
    //数量格式化
    public static String numFormat2(Long number,Boolean isExcel){
        if(number == null || number == 0) {
            return "0";
        }
        if(isExcel){
            return String.valueOf(number);
        }
        DecimalFormat df = new DecimalFormat("###,###");
        return df.format(number);
    }
    //差额格式化
    public static String differenceFormat(BigDecimal amount){
        if(amount == null || amount.compareTo(BigDecimal.ZERO)==0) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("###,##0.00");
        //df.setPositivePrefix("+");
        return df.format(amount);
    }
    //差量格式化
    public static String deltaFormat(Long number){
        DecimalFormat df = new DecimalFormat("###,###");
        if(number == null || number == 0) {
            return "0";
        }
        return df.format(number);
    }
    //百分比格式化
    public static String percentFormat(BigDecimal percent){
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMaximumFractionDigits(2);
        nf.setMinimumFractionDigits(2);
        return nf.format(percent);
    }
    //获取指定日期的月份时间戳
    public static String getMonthTimestamp(Date date){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date monthBegin = null;
        try {
            monthBegin = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期转换错误");
        }
        return Optional.ofNullable(monthBegin).map(o -> String.valueOf(o.getTime())).orElse(null);
    }
    //获取指定日期的月份
    public static String getMonth(Date date){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(date);
    }
    //获取指定日期的月份数
    public static int getMonthNum(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        return month;
    }
    //获取指定日期的年份
    public static String getYear(Date date){
        DateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date);
    }
    //计算理论进度值
    public static String getProgressValue(String queryDate){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parse(queryDate));
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int monthDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        BigDecimal progress = new BigDecimal(day).divide(new BigDecimal(monthDays),4,BigDecimal.ROUND_HALF_UP);
        return DomUtil.percentFormat(progress);
    }
    /**
     * 将时间戳转换为日期
     */
    public static String stampToDate(Long s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(s);
        return simpleDateFormat.format(date);
    }

    /**
     *  获取两个时间之间的间隔月份
     * @param startTimeStr  开始时间
     * @param endTimeStr    结束时间
     * @return      月份
     */
    public static List<Date> getBetweenMonthList(String startTimeStr, String endTimeStr) {
        Date startTime = strToDateLong(startTimeStr);
        Date endTime = strToDateLong(endTimeStr);

        List<Date> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        while(startTime.getTime() <= endTime.getTime()) {
            result.add(startTime);
            calendar.setTime(startTime);
            calendar.add(Calendar.MONTH, 1);
            startTime = calendar.getTime();
        }
        return result;
    }

    /**
     *  获取两个时间之间的间隔月份数
     * @param startTimeStr  开始时间
     * @param endTimeStr    结束时间
     * @return      月份数
     */
    public static int getBetweenMonths(String startTimeStr, String endTimeStr) {
        Date startTime = strToDateLong(startTimeStr);
        Date endTime = strToDateLong(endTimeStr);
        return getMonthNum(endTime)-getMonthNum(startTime);
    }

    /**
     *  获取两个时间之间的间隔天数
     * @param startTimeStr  开始时间
     * @param endTimeStr    结束时间
     * @return      天数          例如2018-11-01 00:00:00至2018-11-30 23:59:59  返回为30
     */
    public static int getBetweenDays(String startTimeStr, String endTimeStr) {
        int betweenDays = 0;
        Date startTime = strToDateLong(startTimeStr);
        Date endTime = strToDateLong(endTimeStr);

        long start = startTime.getTime();
        long end = endTime.getTime();

        betweenDays = (int) (Math.abs(end - start)/(24*3600*1000));

        return betweenDays + 1;
    }

    public static long getBetweenSeconds(String startTimeStr, String endTimeStr) {
        Date startTime = DateUtils.string2Date(startTimeStr,DateUtils.DATETIME_FORMAT);
        Date endTime = DateUtils.string2Date(endTimeStr,DateUtils.DATETIME_FORMAT);
        long start = startTime.getTime();
        long end = endTime.getTime();
        long betweenSeconds = (end - start)/1000;
        return betweenSeconds;
    }

    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 获取 当月 某天
     * @param day
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDayStr(int day, String time) {
        //获得当前时间的本月十号
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.formatString2Date(time, DateUtils.DATE_FORMAT));
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.add(Calendar.MONTH, 0);
        // 时
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // 分
        calendar.set(Calendar.MINUTE, 0);
        // 秒
        calendar.set(Calendar.SECOND, 0);
        // 毫秒
        calendar.set(Calendar.MILLISECOND, 0);
        // 时间格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 获取 当月 某天
     * @param day
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static Date getDate(int day, String time) {
        //获得当前时间的本月十号
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.formatString2Date(time, DateUtils.DATE_FORMAT));
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.add(Calendar.MONTH, 0);
        // 时
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // 分
        calendar.set(Calendar.MINUTE, 0);
        // 秒
        calendar.set(Calendar.SECOND, 0);
        // 毫秒
        calendar.set(Calendar.MILLISECOND, 0);
        // 时间格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 获取传入日期所在月的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonth(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, last);

        // 时
        cal.set(Calendar.HOUR_OF_DAY, 0);
        // 分
        cal.set(Calendar.MINUTE, 0);
        // 秒
        cal.set(Calendar.SECOND, 0);

        return cal.getTime();

    }

    /**
     * 获取传入日期所在月的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, last);

        // 时
        cal.set(Calendar.HOUR_OF_DAY, 23);
        // 分
        cal.set(Calendar.MINUTE, 59);
        // 秒
        cal.set(Calendar.SECOND, 59);

        return cal.getTime();

    }
    //获取当月格式yyyy-MM
    public static String getThisMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        date = calendar.getTime();
        return format.format(date);
    }
    //获取上月格式yyyy-MM
    public static String getLastMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.MONTH,-1);
        date = calendar.getTime();
        return format.format(date);
    }
    //获取指定日期上月格式yyyy-MM
    public static String getLastMonthByDate(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.MONTH,-1);
        date = calendar.getTime();
        return format.format(date);
    }

    public static Long divide(Long strA, int strB) {
        return new BigDecimal(strA).divide(new BigDecimal(strB), BigDecimal.ROUND_CEILING).longValue();
    }

    public static String convertDate(String date){
        return date.substring(8,10);
    }

    public static void main(String[] args) {
        BigDecimal aaa = new BigDecimal("0.12");
        System.out.println(amountFormatExcel(aaa,false));

    }

}
